package com.scpii.universal.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Resource;

import org.apache.commons.lang.xwork.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.AppPartnerDao;
import com.scpii.universal.hibernate.entity.AppPartnerTab;
import com.scpii.universal.service.CatelogInterface;
import com.scpii.universal.service.CrontabService;
import com.scpii.universal.util.EhCacheManager;

@Service
public class CrontabServiceImpl implements CrontabService {
	@Resource
	private CatelogInterface catelogInterface;
	@Resource
	private AppPartnerDao appPartnerDao;
	private Log log = LogFactory.getLog(this.getClass());

	private class TaskHandler extends Thread {
		private AppPartnerTab appPartnerTab;

		private TaskHandler(AppPartnerTab appPartnerTab) {
			this.appPartnerTab = appPartnerTab;
		}

		@Override
		public void run() {
			catelogInterface.save(appPartnerTab);
		}

	}

	@Override
//	@Scheduled(fixedDelay=3000)
	@Scheduled(cron="0 0 1 * * ?")//秒 分 小时 日期 月份  星期 年(可选)
	public void work() {
		if (!"true".equals(EhCacheManager.getObject(GenericConstants.CRONTAB_FLAG_ON).toString()))
			return;
		log.info("********定时调用任务开始********");
		Set<PartnerEntity> tasks = getAllTask();
		if (tasks != null && tasks.size() > 0) {
			ExecutorService pool = Executors.newFixedThreadPool(tasks.size());
			for (Iterator<PartnerEntity> iterator = tasks.iterator(); iterator.hasNext();) {
				PartnerEntity partnerEntity = iterator.next();
				pool.execute(new TaskHandler(partnerEntity.getAppPartnerTab()));
			}
		} else {
			log.info("********定时调用任务结束，没有可调用接口********");
		}
	}
	
	/**
	 * 获取所有需要调用的接口
	 * @author Taylor
	 * 2012-10-12 下午02:05:26
	 * @return
	 * Set<PartnerEntity>
	 */
	private Set<PartnerEntity> getAllTask() {
		StringBuffer hql = new StringBuffer("from AppPartnerTab a ");
		hql.append("where a.intDeleteSign=:intDeleteSign");
		Map<String, Object> argsMap = new HashMap<String, Object>();
		argsMap.put("intDeleteSign", GenericConstants.DELETE_SIGN_EXIST);
		List<AppPartnerTab> appPartnerTabs = appPartnerDao.hqlGetList(hql.toString(), argsMap);
		Set<PartnerEntity> tasks = new HashSet<PartnerEntity>();
		if (appPartnerTabs != null && appPartnerTabs.size() > 0) {
			for (AppPartnerTab appPartnerTab : appPartnerTabs) {
				if (StringUtils.isBlank(appPartnerTab.getVarInterfaceUrl()))
					continue;
				PartnerEntity entity = new PartnerEntity(1, appPartnerTab);
				//应用ID和接口地址都一样时，只更新其中一个
				tasks.add(entity);
			}
		}
		return tasks;
	}
	
	private class PartnerEntity {
		private int id;
		private AppPartnerTab appPartnerTab;

		private PartnerEntity(int id, AppPartnerTab appPartnerTab) {
			super();
			this.id = id;
			this.appPartnerTab = appPartnerTab;
		}

		@Override
		public boolean equals(Object obj) {
			PartnerEntity o = (PartnerEntity) obj;
			AppPartnerTab part = o.getAppPartnerTab();
			if (part.getIntAppId().intValue() == appPartnerTab.getIntAppId().intValue()
					&& (part.getVarInterfaceUrl().trim()).equals((appPartnerTab
							.getVarInterfaceUrl().trim())))
				return true;
			return false;
		}

		@Override
		public int hashCode() {
			return id;
		}

		public AppPartnerTab getAppPartnerTab() {
			return appPartnerTab;
		}

	}
}
